//
//  GridItemView.swift
//  BasicTools
//
//  Created by ikang on 2025/8/29.
//

import SwiftUI

struct GridItemView: View {
    @Environment(GridListModel.self) var listModel
    @Bindable var itemModel: GridItemModel
    var currentIndex: Int = 0
    
    var body: some View {
        if listModel.itemType == .itemVStack {
            VStack(spacing: listModel.itemSpace) { 
                Image(itemModel.imageStr)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: listModel.imageSize.width, height: listModel.imageSize.height)
                
                Text(itemModel.title)
                    .font(listModel.titleFont)
                    .foregroundStyle(isSelect() ? listModel.selectTitleColor : listModel.titleColor)
            }
            .frame(maxWidth: .infinity, minHeight: listModel.itemHeight > 0 ? listModel.itemHeight : 0, maxHeight: listModel.itemHeight > 0 ? listModel.itemHeight : .infinity)
            .bt.viewBorder(cornerRadius: listModel.itemCornerRadius, 
                           borderColor: isSelect() ? listModel.itemSelectBorderColor : listModel.itemBorderColor,
                           borderWidth: isSelect() ? listModel.itemSelectBorderWidth : listModel.itemBorderWidth,
                           backgroundColor: isSelect() ? listModel.itemSelectBackgroundColor : listModel.itemBackgroundColor)
            .contentShape(Rectangle())
        }else if listModel.itemType == .itemHStack {
            HStack(spacing: listModel.itemSpace) { 
                if !itemModel.imageStr.isEmpty {
                    Image(itemModel.imageStr)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: listModel.imageSize.width, height: listModel.imageSize.height)
                }
                
                Text(itemModel.title)
                    .font(listModel.titleFont)
                    .foregroundStyle(isSelect() ? listModel.selectTitleColor : listModel.titleColor)
            }
            .frame(maxWidth: .infinity, minHeight: listModel.itemHeight > 0 ? listModel.itemHeight : 0, maxHeight: listModel.itemHeight > 0 ? listModel.itemHeight : .infinity)
            .bt.viewBorder(cornerRadius: listModel.itemCornerRadius, 
                           borderColor: isSelect() ? listModel.itemSelectBorderColor : listModel.itemBorderColor,
                           borderWidth: isSelect() ? listModel.itemSelectBorderWidth : listModel.itemBorderWidth,
                           backgroundColor: isSelect() ? listModel.itemSelectBackgroundColor : listModel.itemBackgroundColor)
            .contentShape(Rectangle())
        }else {
            EmptyView()
        }
    }
    
    func isSelect() -> Bool {
        if listModel.isCanSelect, listModel.selectIndex == currentIndex {
            return true
        }
        return false
    }
    
}

//#Preview {
//    SwiftUIView()
//}
